<?php
/*
 * 配送清单接口
 */
class Apiv1_DispatchingController extends Ctrl_Api{
    public function indexAction(){
        $p = $_REQUEST;
        $pDepartment = empty($p['department'])?'':$pDepartment;
        $pId = empty($p['id'])?0:intval($p['id']);
        $pOpt = empty($p['opt'])?'':trim($p['opt']);
        $tOptarr = array('up','down');
        $pSize = empty($p['size'])?5:intval(trim($p['size']));
        
        $tAMO = new ApplylistModel;
        $tDMO = new DispatchingModel;
        $tAdMO = new AdminModel;
        $tWMO = new WorkerModel;
        $tDIMO = new DepartmentinfoModel;

        $tTime = time();

		if(!in_array($pOpt,$tOptarr)){Tool_Fnc::appMsg('操作类型错误');}

        $tWhere = ' 1 ';

        if(!empty($pDepartment)){
            $tWhere .= ' and w.department = '.$pDepartment;
        }

        if($pOpt == 'up'){
            $tWhere .= ' and id < '.$pId;
        }else{
            $tWhere .= ' and id > '.$pId;
        }

        $tCount = $tDMO->field('count(id) c')->where($tWhere)->fList();
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];

        $tDatas = array();
        if(!empty($tCnt)){
            $tDatas = $tDMO->field('id,a_id,status')->where($tWhere)->order('id desc')->limit('0,'.$pSize)->fList();
            if(!empty($tDatas)){
                foreach($tDatas as $tK=>$tV){
                    $tAData = $tAMO->field('id,apply_name,apply_num,apply_time,apply_com')->where('apply_id = '.$tV['a_id'])->fRow();
                    $tDatas[$tK]['apply_name'] = empty($tAData['apply_name'])?'':$tAData['apply_name'];
                    $tDatas[$tK]['apply_num'] = empty($tAData['apply_num'])?'':$tAData['apply_num'];
                    $tDatas[$tK]['apply_time'] = empty($tAData['apply_time'])?'':$tAData['apply_time'];
                    $tDatas[$tK]['apply_com'] = empty($tAData['apply_com'])?'':$tAData['apply_com'];
                    //获取部门
                    $tSql = 'select di.introduce department from '.$tAdMO->table.' ad left join '.$tWMO->table.' w on w.id = ad.w_id left join '.$tDIMO->table.' di on w.department = di.num where ad.id = '.$tAData['id'];
                    $tAdData = $tAdMO->query($tSql);
                    $tDatas[$tK]['department'] = empty($tAdData[0]['department'])?'':$tAdData[0]['department'];
                }
            }
        }

        Tool_Fnc::ajaxMsg('',1,$tDatas);

    }
    public function editAction(){
        $p = $_REQUEST;

        $tAMO = new ApplylistModel;
        $tDMO = new DispatchingModel;
        $tAdMO = new AdminModel;
        $tWMO = new WorkerModel;
        $tDIMO = new DepartmentinfoModel;

        $tTime = time();

            $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('ID出错'):Tool_Fnc::safe_string($p['id']);
            $pAid = empty($p['aid'])?Tool_Fnc::ajaxMsg('aid出错'):Tool_Fnc::safe_string($p['aid']);

            $tData = array(
                'id' => $pId,
                'status' => 2,
                'updated' => $tTime, 
            );
            $tDMO->begin();
            if(!$tDMO->update($tData)){
                Tool_Fnc::ajaxMsg('操作失败');
            }
            //更新采购清单状态
            $tData = array(
                'apply_id' => $pAid,
                'sta_id' => 4,
            );
            if(!$tAMO->update($tData)){
                $tDMO->back();
                Tool_Fnc::ajaxMsg('操作失败 002');
            }
            $tDMO->commit();
            Tool_Fnc::ajaxMsg('操作成功',1);

    }
}
?>
